from PySide6.QtWidgets import QMainWindow,QApplication
from PySide6.QtCore import QRunnable,Slot,QThreadPool
import sys,time

from Ui_main import Ui_MainWindow

# 自定义Worker类
class Worker(QRunnable):
    def __init__(self, fn, *args, **kwargs) -> None:
        super().__init__()
        self.fn = fn
        self.args = args
        self.kwargs = kwargs
        
    @Slot()
    def run(self):
        self.fn(*self.args, **self.kwargs)
    

class Main(QMainWindow):
    def __init__(self) -> None:
        super().__init__()
        # 加载自定义Ui布局
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        
        # 创建线程池
        self.threadpool = QThreadPool()
        
        # 绑定按钮的点击事件
        self.ui.pushButton_check.clicked.connect(self.hello)
        
    # Hello World!事件
    # def hello(self):
    #     def printText(text,delay=1):
    #         for i in range(len(text)+1):
    #             self.ui.lineEdit_check.setText(text[:i])
    #             QApplication.processEvents()
    #             time.sleep(delay*(1/(len(text)+1)))
    #     printText("Hello World!")

        
    # Hello World!事件
    def hello(self):
        def printText(text,delay=1):
            for i in range(len(text)+1):
                self.ui.lineEdit_check.setText(text[:i])
                time.sleep(delay*(1/(len(text)+1)))
                
        worker = Worker(printText,"Hello World!")
        
        # 将worker加入线程池，避免UI假死
        self.threadpool.start(worker)
        

if __name__ == '__main__':
    app = QApplication()
    main = Main()
    main.show()
    app.exec()
